﻿Imports System.Data.SqlClient
Imports System.Configuration

Public Class frmSettings
    Private Sub Settings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable = Functions.DoQueryReturnDT("Select * from _OCNT")
        If IsNothing(dt) Then
            cmb_Database.Text = PublicVariable.oCompany.CompanyDB
            txt_SAPUser.Text = PublicVariable.oCompany.UserName
            txt_SAPPass.Text = ""
            txt_SQLServer.Text = PublicVariable.oCompany.Server
            txt_UserName.Text = "sa"
            txt_Password.Text = ""
            txt_LicenseServer.Text = PublicVariable.oCompany.LicenseServer
            cbSQLType.Text = "2008"
        Else
            If dt.Rows.Count > 0 Then
                'SAP_YGN;manager;4321;PC\WIN764;sa;win764;PC;2008
                Dim MyArr As Array = oMD5.DecryptPassword(dt.Rows(0).Item("CnnStr").ToString).Split(";")
                cmb_Database.Text = MyArr(0).ToString()
                txt_SAPUser.Text = MyArr(1).ToString()
                txt_SAPPass.Text = MyArr(2).ToString()
                txt_SQLServer.Text = MyArr(3).ToString()
                txt_UserName.Text = MyArr(4).ToString()
                txt_Password.Text = MyArr(5).ToString()
                txt_LicenseServer.Text = MyArr(6).ToString()
                cbSQLType.Text = MyArr(7).ToString()
            Else
                cmb_Database.Text = PublicVariable.oCompany.CompanyDB
                txt_SAPUser.Text = PublicVariable.oCompany.UserName
                txt_SAPPass.Text = ""
                txt_SQLServer.Text = PublicVariable.oCompany.Server
                txt_UserName.Text = "sa"
                txt_Password.Text = ""
                txt_LicenseServer.Text = PublicVariable.oCompany.LicenseServer
                cbSQLType.Text = "2008"
            End If
        End If
    End Sub
    Private Sub btn_Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Ok.Click
        Try
            Dim str As String = ""
            str = cmb_Database.Text + ";" + txt_SAPUser.Text + ";" + txt_SAPPass.Text + ";" + txt_SQLServer.Text + ";" + txt_UserName.Text + ";" + txt_Password.Text + ";" + txt_LicenseServer.Text + ";" + cbSQLType.Text
            str = oMD5.EncryptPassword(str)
            str = str.Replace("'", "''")
            Dim ret As String = ""
            Functions.DoQueryReturnDT("if (select COUNT(*) from _OCNT)>0 update _OCNT set CnnStr='" + str + "' else insert into dbo._OCNT values ('" + str + "')")

            MessageBox.Show("Operation Complete!")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
    Private Sub cmb_Database_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_Database.DropDown
        Dim table As DataTable = New DataTable("DBName")
        table.Columns.Add("Name")
        Dim str As String = String.Format("Data Source={0};User ID={1};Password={2}", txt_SQLServer.Text, txt_UserName.Text, txt_Password.Text)
        Dim oldValue As String = cmb_Database.Text
        Dim sqlConx As SqlConnection = New SqlConnection(str)
        Try

            sqlConx.Open()
            Dim tblDatabases As DataTable = sqlConx.GetSchema("Databases")
            sqlConx.Close()

            For Each row As DataRow In tblDatabases.Rows
                table.Rows.Add(row("database_name"))
            Next
            cmb_Database.DataSource = table
            cmb_Database.DisplayMember = "Name"
            cmb_Database.ValueMember = "Name"
            cmb_Database.Text = ""
            cmb_Database.SelectedText = oldValue
        Catch
            MessageBox.Show("Can not get database list", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try

    End Sub
    Private Sub btnTestLocal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestLocal.Click
        Me.Cursor = Cursors.WaitCursor
        Dim sStr As String = ""
        Dim lErrCode As Integer
        Dim sErrMsg As String
        Dim TestCompany As SAPbobsCOM.Company = New SAPbobsCOM.Company
        TestCompany.CompanyDB = cmb_Database.Text
        TestCompany.UserName = txt_SAPUser.Text
        TestCompany.Password = txt_SAPPass.Text
        TestCompany.Server = txt_SQLServer.Text
        TestCompany.DbUserName = txt_UserName.Text
        TestCompany.DbPassword = txt_Password.Text
        TestCompany.LicenseServer = txt_LicenseServer.Text
        Select Case cbSQLType.Text
            Case "2008"
                TestCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008
            Case "2005"
                TestCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2005
            Case "2012"
                TestCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2012
        End Select

        lErrCode = TestCompany.Connect
        If lErrCode <> 0 Then
            TestCompany.GetLastError(lErrCode, sErrMsg)
            MessageBox.Show(sErrMsg)
        Else
            TestCompany.Disconnect()
            MessageBox.Show("Connect to SAP successful!")
        End If
        Me.Cursor = Cursors.Default
    End Sub

   
End Class